import { constantRoutes } from '@/router/routes/constantRoutes'
import { asyncRoutes } from '@/router/routes/asyncRoutes'

const actions = {
  addRoutesAction({ commit }, userRoutes) {
    /* 根据用户本身页面路由权限数组和项目中所有的异步路由进行过滤生成最终路由，并且返回出去使用addRoutes([最终路由])动态添加到路由表 */
    const resultRoutes = asyncRoutes.filter(item => userRoutes.includes(item.children[0].name))
    commit('updateRoutesMutation', resultRoutes)
    return resultRoutes
  }
}
const mutations = {
  updateRoutesMutation(state, resultRoutes) {
    state.routes = [...constantRoutes, ...resultRoutes]
  }
}
const state = () => {
  return {
    routes: constantRoutes
  }
}
const getters = {}
export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
